<?php

namespace App\Model\Dao;

use App\Utils\CustomUtils;
use App\Concern\BaseDao;
use Hyperf\Database\Query\Builder;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\Arr;
use Redis;
use App\Utils\RedisUtils;
use Hyperf\Utils\Codec\Json;

/**
 * CurrencyCodeDao
 */
class CurrencyCodeDao extends BaseDao {

    /**
     * 表名常量
     * 联表的时候通过XxxDao::TABLE获取
     */
    const TABLE = "currency_code";

    // 不支持软删除
    protected $softDelete = null;

    /**
     * 父类中通过静态函数获取表名, 因为不能通过静态常量获取, 这个函数是固定写在子类中的
     */
    public static function table (): string {
        return self::TABLE;
    }

    /**
     * 排序
     *
     * @param Builder $builder
     * @param array   $params
     *
     * @return Builder|mixed
     */
    protected function _buildOrder (Builder $builder, $params = []) {

        $builder->orderBy('main.sort', 'desc');
        $builder->orderBy('main.code', 'asc');


        return parent::_buildOrder($builder, $params);
    }

    /**
     * 查询条件
     *
     * @param $params
     *
     * @return Builder
     */
    protected function _buildQueryString ($params) {
        // 本表一律命名为main
        $main = sprintf("%s AS main", self::TABLE);

        // 联表表明获取
        //$country = sprintf("%s AS country", CountryDao::TABLE);

        $builder = DB::table($main);

        // 通过 Arr::exists 来判断是否存在该键, isset不准确
        Arr::exists($params, 'isBoc') && $builder->where('main.isBoc', $params['isBoc']);
        Arr::exists($params, 'code') && $builder->where('main.code', 'like', "%{$params['code']}%");
        Arr::exists($params, 'name') && $builder->where('main.name', 'like', "%{$params['name']}%");

        return $builder;
    }


}
